import Vue from 'vue'
import VueRouter from 'vue-router'
Vue.use(VueRouter)
import Index from '@/pages/index'
import Bottom from '@/pages/Dishes/Bottom'
import Order from '@/pages/Order'
import Home from '@/pages/Home'
import Login from '@/pages/Login'
import Register from '@/pages/Register'
import User from '@/pages/User'
import Foods from '@/pages/Foods'
import food from '@/pages/food'
import HavePaid from '@/pages/Order/HavePaid'
import NoPaid from '@/pages/Order/NoPaid'
import OrderDetails from '@/pages/Order/OrderDetails'
// 先把VueRouter原型对象的push保存一份
let originPush = VueRouter.prototype.push
// 重写push||replace
// 第一个参数：告诉原来push方法,往哪儿跳(传递哪些参数)
VueRouter.prototype.push = function (location, resolve, reject) {
  if (resolve && reject) {
    originPush.call(this, location, resolve, reject)
  } else {
    originPush.call(this, location, () => { }, () => { })
  }
}
const router = new VueRouter({
  routes: [
    {
      path:'/',
      redirect:'/index/home'
    },
    {
      path: '/index',
      redirect:'/index/home',
      component: Index,
      children: [
        {
          redirect: '/index/foods/bottom/1',
          path: 'foods',
          component: Foods,
          children: [
            {
              path: 'bottom/:num',
              component: Bottom
            },
          ]
        },
        {
          redirect: '/index/order/nopaid',
          path: 'order',
          component: Order,
          children: [

            {
              path: 'havepaid',
              component: HavePaid,
              meta: {
                flag: true
              }
            },
            {
              path: 'nopaid',
              component: NoPaid,
              meta: {
                flag: true
              }
            },
            {
              path: 'orderdetails',
              component: OrderDetails
            }
          ]
        },
        {
          path: 'home',
          component: Home
        }, {
          path: 'user',
          component: User,
          meta: {
            flag: true
          }
        }
      ]
    },
    {
      path: '/login',
      component: Login
    },
    {
      path: '/register',
      component: Register
    },
    {
      path: '/food',
      component: food
    }
  ]
})
router.beforeEach((to, from, next) => {
  if (to.meta.flag === true) {
    if (localStorage.getItem('name')) {
      next()
    } else {
      alert('请先登录')
      next('/login')
    }
  } else {
    next()
  }
})
export default router
